<?php
/**
 * User: hufeng
 * Date: 2017/10/11
 * Desc: 权限管理
 */

namespace Admin\Controller;
use Model\AdminGroupModel;
use Model\AdminCustomerModel;
use Model\AdminMenuModel;
use Model\UserModel;
use Common\Util\HashId;

class PowerController extends BaseController{

    public $groupModel;
    public $userModel;
    public $uModel;

    public function __construct(){
        parent::__construct();
        $this->groupModel = new AdminGroupModel();
        $this->userModel = new AdminCustomerModel();
        $this->uModel = new UserModel();
    }

    //管理员列表
    public function index(){
        if($_POST['__hash__'] || !$_GET['p']){
            session('searchParam',null);
        }
        $sessionParam = session('searchParam');

        $sessionArr['status']  = @$_POST['status'] ? $_POST['status'] : @$sessionParam['status'];
        $sessionArr['name']  = @$_POST['name'] ? $_POST['name'] : @$sessionParam['name'];
        $sessionArr['group']  = @$_POST['group'] ? $_POST['group'] : @$sessionParam['group'];
        $sessionArr['start']  = @$_POST['start'] ? $_POST['start'] : @$sessionParam['start'];
        $sessionArr['end']  = @$_POST['end'] ? $_POST['end'] : @$sessionParam['end'];
        session('searchParam',$sessionArr);
        $page = @$_GET['p'] ? $_GET['p'] : 1;

        $where = [];
        $where['status'] = array('neq',3);
        if($sessionArr['status']){
            $where['status'] = $sessionArr['status'];
        }
        if($sessionArr['name']){
            $where['tel'] = array('like','%'.$sessionArr['name'].'%');
        }
        if($sessionArr['group']){
            $gRes = $this->groupModel->findData(['name'=>$sessionArr['group']]);
            $gid = $gRes['id'].',';
            $where['agids'] = array('like','%'.$gid.'%');
        }
        if($sessionArr['start'] && $sessionArr['end']){
            $where['_string'] = "addtime>='{$sessionArr['start']}' and addtime<='{$sessionArr['end']} 23:59:59'";
        }

        $resList = $this->userModel->search($where,$page,$this->pernum);
        $totalNum = $this->userModel->getTotalNum($where);
        //分组
        $allGroup = $this->groupModel->allGroup();
        foreach($resList as &$val){
            $menu_ids = rtrim($val['agids'],',');
            $menuarr = explode(',',$menu_ids);
            $gStr = '';
            foreach($menuarr as $mval){
                $gStr .= $allGroup[$mval].',';
            }
            $val['group_name'] = rtrim($gStr,',');
        }
        $pageShow = getPage($totalNum,$this->pernum);
        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$resList);
        $this->assign('totalNum',$totalNum);
        $this->assign('postArr',$sessionArr);
        $this->display('power/index');
    }

    //新增管理员
    public function addMg(){
        if(IS_POST){
            $uRes = $this->userModel->findData(['tel'=>$_POST['name']]);
            if($uRes['id']){
                redirect($this->domianUrl.'/power/index?tip=2&errmsg='.urlencode(urlencode('账号已存在')));
            }
            $uData = [
                'nick_name' => $_POST['name'],
                'tel' => 0,
                'passwd' => md5($_POST['passwd']),
                'addtime' => date('Y-m-d H:i:s'),
                'source' => 2
            ];
            $uid = $this->uModel->addData($uData);
            $openId = HashId::build($uid);
            $this->uModel->editData(['id'=>$uid],['open_id'=>$openId]);

            $agids = implode(',',$_POST['groupId']).',';
            $data['name'] = $_POST['name'];
            $data['tel'] = $_POST['name'];
            $data['passwd'] = md5($_POST['passwd']);
            $data['agids'] = $agids;
            $data['uid'] = $uid;
            $id = $this->userModel->addData($data);


            $this->logModel->log('添加管理员','admin_customer',$id);
            redirect($this->domianUrl.'/power/index?tip=1');
        }

        //获取分组
        $groupData = $this->groupModel->selectData(['status'=>['neq',3]],'id,name');
        $this->assign('groupData',$groupData);
        $this->display('power/addmg');
    }
    //编辑管理员
    public function editMg(){
        if(IS_POST){
            $agids = implode(',',$_POST['groupId']).',';
            $uRes = $this->userModel->findData(['tel'=>$_POST['name']]);
            if($uRes['id'] && $uRes['id'] != $_POST['id']){
                redirect($this->domianUrl.'/power/index?tip=2&errmsg='.urlencode(urlencode('账号已存在')));
            }
            $where['id'] = $_POST['id'];
            $data['name'] = $_POST['name'];
            $data['tel'] = $_POST['name'];
            if($_POST['passwd']){
                $data['passwd'] = md5($_POST['passwd']);
            }
            $data['agids'] = $agids;
            $this->userModel->editData($where,$data);
            $uRes = $this->userModel->findData(['id'=>$_POST['id']]);
            $this->uModel->editData(['id'=>$uRes['uid']],['nick_name'=>$_POST['name']]);
            $this->logModel->log('编辑管理员','admin_customer',$_POST['id']);
            redirect($this->domianUrl.'/power/index?p='.$_POST['page'].'&tip=1');
        }
        //获取组内容
        $this->assign('page',$_GET['page']);
        $selfInfo = $this->userModel->findData(['id'=>$_GET['id']]);
        $this->assign('selfInfo',$selfInfo);

        //获取分组
        //获取分组
        $groupData = $this->groupModel->selectData(['status'=>['neq',3]],'id,name');
        $this->assign('groupData',$groupData);
        $this->display('power/editmg');
    }

    //冻结解冻管理员
    public function freezeMg(){
        $data['status'] = $_GET['status'] == 1 ? 2: 1;
        $where['id'] = $_GET['id'];
        $this->userModel->editData($where,$data);
        $this->logModel->log('冻结|解冻管理员','admin_customer',$_GET['id']);
        redirect($this->domianUrl.'/power/index?p='.$_GET['page'].'&tip=1');
    }
    //删除管理员
    public function delMg(){
        $data['status'] = 3;
        $where['id'] = $_GET['id'];
        $this->userModel->editData($where,$data);
        $this->logModel->log('删除管理员','admin_customer',$_GET['id']);
        redirect($this->domianUrl.'/power/index?p='.$_GET['page'].'&tip=1');
    }


    //权限组列表
    public function groupList(){
        if($_POST['__hash__'] || !$_GET['p']){
            session('searchParam',null);
        }
        $sessionParam = session('searchParam');

        $sessionArr['status']  = @$_POST['status'] ? $_POST['status'] : @$sessionParam['status'];
        $sessionArr['name']  = @$_POST['name'] ? $_POST['name'] : @$sessionParam['name'];
        $sessionArr['start']  = @$_POST['start'] ? $_POST['start'] : @$sessionParam['start'];
        $sessionArr['end']  = @$_POST['end'] ? $_POST['end'] : @$sessionParam['end'];
        session('searchParam',$sessionArr);
        $page = @$_GET['p'] ? $_GET['p'] : 1;

        $where = [];
        $where['status'] = array('neq',3);
        if($sessionArr['status']){
            $where['status'] = $sessionArr['status'];
        }
        if($sessionArr['name']){
            $where['name'] = array('like','%'.$sessionArr['name'].'%');
        }
        if($sessionArr['start'] && $sessionArr['end']){
            $where['_string'] = "addtime>='{$sessionArr['start']}' and addtime<='{$sessionArr['end']} 23:59:59'";
        }

        $resList = $this->groupModel->search($where,$page,$this->pernum);
        $totalNum = $this->groupModel->getTotalNum($where);

        $pageShow = getPage($totalNum,$this->pernum);
        $this->assign('pageShow',$pageShow);
        $this->assign('page',$page);
        $this->assign('resList',$resList);
        $this->assign('totalNum',$totalNum);
        $this->assign('postArr',$sessionArr);
        $this->display('power/grouplist');
    }
    //新增权限组
    public function addGroup(){
        if(IS_POST){
            $menuIds = implode(',',$_POST['menu']);
            $groupData['name'] = $_POST['name'];
            $groupData['menu_ids'] = $menuIds;
            $groupId = $this->groupModel->addData($groupData);
            $this->logModel->log('添加权限组','admin_group',$groupId);
            redirect($this->domianUrl.'/power/groupList?tip=1');
        }
        //获取所有可用菜单
        $menuModel = new AdminMenuModel();
        $menuList = $menuModel->selectData(['status'=>1],'id,name,pid,platform');
        $menuArr = [];
        $platForm = C('PLATFORM');
        foreach($menuList as $val){
            $arr = [];
            $arr['id'] = $val['id'];
            $arr['name'] = $val['name'];
            if($val['pid']==0){
                $menuArr[$val['platform']][$val['id']] = $arr;
            }else{
                $menuArr[$val['platform']][$val['pid']]['child'][] = $arr;
            }
        }
        $this->assign('platForm',$platForm);
        $this->assign('menuArr',$menuArr);
        $this->display('power/addgroup');
    }
    //编辑权限组
    public function editGroup(){
        if(IS_POST){
            $where['id'] = $_POST['id'];
            $menuIds = implode(',',$_POST['menu']);
            $groupData['name'] = $_POST['name'];
            $groupData['menu_ids'] = $menuIds;
            $this->groupModel->editData($where,$groupData);
            $this->logModel->log('编辑权限组','admin_group',$_POST['id']);
            redirect($this->domianUrl.'/power/groupList?&p='.$_POST['page'].'&tip=1');
        }

        $this->assign('page',$_GET['page']);
        $selfInfo = $this->groupModel->findData(['id'=>$_GET['id']]);
        $this->assign('selfInfo',$selfInfo);

        //获取所有可用菜单
        $menuModel = new AdminMenuModel();
        $menuList = $menuModel->selectData(['status'=>1],'id,name,pid,platform');
        $menuArr = [];
        $platForm = C('PLATFORM');
        foreach($menuList as $val){
            $arr = [];
            $arr['id'] = $val['id'];
            $arr['name'] = $val['name'];
            if($val['pid']==0){
                $menuArr[$val['platform']][$val['id']] = $arr;
            }else{
                $menuArr[$val['platform']][$val['pid']]['child'][] = $arr;
            }
        }
        $this->assign('platForm',$platForm);
        $this->assign('menuArr',$menuArr);
        $this->display('power/editgroup');
    }
    //冻结解冻权限组
    public function freezeGroup(){
        $data['status'] = $_GET['status'] == 1 ? 2: 1;
        $where['id'] = $_GET['id'];
        $this->groupModel->editData($where,$data);
        $this->logModel->log('冻结|解冻权限组','admin_group',$_GET['id']);
        redirect($this->domianUrl.'/power/groupList?p='.$_GET['page'].'&tip=1');
    }
    //删除权限组
    public function delGroup(){
        $data['status'] = 3;
        $where['id'] = $_GET['id'];
        $this->groupModel->editData($where,$data);
        $this->logModel->log('删除权限组','admin_group',$_GET['id']);
        redirect($this->domianUrl.'/power/groupList?p='.$_GET['page'].'&tip=1');
    }

}